<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        function user(name){
            this.name=name;
        this.show=function(){
            console.log(this.name);
        }
        }
        //上面的show代码相当于下面这个方法
        user.prototype={
            constructor:user,//构造器一定要写，不然报错
            data:function(){
                console.log("data:"+this.name);
            }
        }

        //以前创建对象，代码的复用型降低了
        let tw=new user("唐玮");
        console.log(tw.name);//user {name: '唐玮'}
        tw.data();//data:唐玮

        //找到父类的构造器，再创建对象
         //定义一个方法
         function setByObject(obj,...args){//传入 对象 对象的名字
            let  myself=obj.prototype.constructor;
            return new myself(...args);
         }

         //调用方法
      let user1=   setByObject(user,"唐家辉");
      console.log(user1);//user {name: '唐家辉'}
      user1.show();//唐家辉
      user1.data();//data:唐家辉

    </script>
</body>
</html>